﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccontrols;
using PluginInterface;
using Buccaneer;
using System.IO;

namespace NotepadPlugin
{
    public class SaveAction: PluginAction
    {
        public SaveAction(IPlugin plugin)
        {
            base.name = "save";
            base.func = new ActionFunction(this.SaveFile);
            base.pars.Add("path", "the path of the file to save");
            base.exceptions.Add("System.IO.IOException", new ActionFunction(SaveError));
            base.plugin = plugin;
        }

        public void SaveFile(string[] args)
        {
            // dit is de functie
            string filename = "";
            if (args == null || args[0] == "")
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.DefaultExt = "*.txt";
                sfd.Filter = "Tekstbestanden|*.txt|Alle bestanden|*.*";
                sfd.RestoreDirectory = true;
                sfd.Title = "Open een tekstbestand";
                sfd.ValidateNames = true;
                sfd.ShowDialog(this.plugin.MainInterface);
                filename = sfd.FileName;
            }
            else
                filename = args[0];
            StreamWriter sw = new StreamWriter(filename);
            sw.Write((base.plugin.MainInterface as Interface).GetText());
        }

        public void SaveError(string[] args)
        {
            Options.WriteComment("Er ging iets mis bij het opslaan van het bestand.");
        }
    }
}
